<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      handler() {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      const data = this.chartData.length > 0 ? this.chartData : [
        { value: 320, name: 'Industries' },
        { value: 240, name: 'Technology' },
        { value: 149, name: 'Forex' },
        { value: 100, name: 'Gold' },
        { value: 59, name: 'Forecasts' }
      ]

      // 分离岗位数据和报名数据
      const jobData = data.filter(item => item.name.includes('岗位'));
      const applyData = data.filter(item => item.name.includes('报名'));

      // 计算岗位数据的总和
      const jobTotal = jobData.reduce((sum, item) => sum + item.value, 0);

      // 计算报名数据的总和
      const applyTotal = applyData.reduce((sum, item) => sum + item.value, 0);

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: function(params) {
            let total = 0;
            if (params.seriesName === '岗位统计') {
              total = jobTotal;
            } else if (params.seriesName === '报名统计') {
              total = applyTotal;
            }
            const percent = total > 0 ? ((params.value / total) * 100).toFixed(2) : 0;
            return `${params.seriesName}<br/>${params.name} : ${params.value} (${percent}%)`;
          }
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['岗位统计', '报名统计']
        },
        series: [
          {
            name: '岗位统计',
            type: 'pie',
            radius: ['40%', '60%'],
            center: ['50%', '38%'],
            data: jobData.map(item => {
              return {
                ...item,
                percent: jobTotal > 0 ? ((item.value / jobTotal) * 100).toFixed(2) : 0
              };
            }),
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          },
          {
            name: '报名统计',
            type: 'pie',
            radius: [0, '30%'],
            center: ['50%', '38%'],
            data: applyData.map(item => {
              return {
                ...item,
                percent: applyTotal > 0 ? ((item.value / applyTotal) * 100).toFixed(2) : 0
              };
            }),
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
